// 表单校验规则由 schema2code 生成，不建议直接修改校验规则，而建议通过 schema2code 生成, 详情: https://uniapp.dcloud.net.cn/uniCloud/schema
import moment from "moment";


const validator = {
  "school_id": {
    "rules": [
      {
        "required": true
      },
      {
        "format": "string"
      }
    ],
    "title": "所属学校",
    "label": "所属学校"
  },
  "send_date": {
    "rules": [
      {
        "required": true
      },
      {
        "format": "string"
      }
    ],
    "title": "送达日期",
    "label": "送达日期"
  },
  "start_time": {
    "rules": [
      {
        "required": true
      },
      {
        "format": "timestamp"
      }
    ],
    "title": "起送时间",
    "label": "起送时间"
  },
  "end_time": {
    "rules": [
      {
        "required": true
      },
      {
        "format": "timestamp"
      }
    ],
    "title": "结束时间",
    "label": "结束时间"
  },
  "name": {
    "rules": [
      {
        "required": true
      },
      {
        "format": "string"
      }
    ],
    "title": "菜单名称",
    "label": "菜单名称"
  },
  "alias_name": {
    "rules": [
      {
        "format": "string"
      }
    ],
    "title": "菜单别名",
    "label": "菜单别名"
  },
  "alias_first_time": {
    "rules": [
      {
        "format": "string"
      }
    ],
    "title": "时间别名",
    "defaultValue": "一小时内送达",
    "label": "时间别名"
  },
  "splice_mode": {
    "rules": [
      {
        "format": "int"
      },
      {
        "range": [
          {
            "value": 0,
            "text": "一小时"
          },
          {
            "value": 1,
            "text": "两小时"
          },
          {
            "value": 2,
            "text": "三小时"
          }
        ]
      }
    ],
    "title": "时间间隔",
    "defaultValue": 0,
    "label": "时间间隔"
  },
  "enable": {
    "rules": [
      {
        "format": "bool"
      }
    ],
    "title": "启用或关闭",
    "defaultValue": true,
    "label": "启用或关闭"
  },
  "splice_time": {
    "rules": [
      {
        "required": true
      },
      {
        "format": "array"
      },
      {
        "arrayType": "int"
      }
    ],
    "title": "右侧菜单",
    "label": "右侧菜单"
  },
  "splice_time_text": {
    "rules": [
      {
        "required": true
      },
      {
        "format": "array"
      },
      {
        "arrayType": "object"
      }
    ],
    "title": "菜单内容",
    "label": "菜单内容"
  }
}

const enumConverter = {
  "splice_mode_valuetotext": {
    "0": "一小时",
    "1": "两小时",
    "2": "三小时"
  }
}

function filterToWhere(filter, command) {
  let where = {}
  for (let field in filter) {
    let { type, value } = filter[field]
    switch (type) {
      case "search":
        if (typeof value === 'string' && value.length) {
          where[field] = new RegExp(value)
        }
        break;
      case "select":
        if (value.length) {
          let selectValue = []
          for (let s of value) {
            selectValue.push(command.eq(s))
          }
          where[field] = command.or(selectValue)
        }
        break;
      case "range":
        if (value.length) {
          let gt = value[0]
          let lt = value[1]
          where[field] = command.and([command.gte(gt), command.lte(lt)])
        }
        break;
      case "date":
        if (value.length) {
          let [s, e] = value
          let startDate = new Date(s)
          let endDate = new Date(e)
          where[field] = command.and([command.gte(startDate), command.lte(endDate)])
        }
        break;
      case "timestamp":
        if (value.length) {
          let [startDate, endDate] = value
          where[field] = command.and([command.gte(startDate), command.lte(endDate)])
        }
        break;
    }
  }
  return where
}

function timeVarformatTime(strVar){
  const nowTime=moment(new Date());
  let showName=nowTime.format(`YYYY-MM-DD`);
  const array=strVar.match(/^\$now([1-9]\d*)*$/);
  const [today,after]=array;
  
  if(after!=null){
    showName=nowTime.set('hour', after*24).format(`YYYY-MM-DD`);
  };
  return showName;
}

export { validator, enumConverter, filterToWhere ,timeVarformatTime}
